package apartmentRentGWT.client;

import java.util.Stack;

import com.google.gwt.core.client.GWT;
import com.google.gwt.resources.client.ImageResource;

/**
 * Created with IntelliJ IDEA.
 * User: LeOn
 * Date: 10/14/12
 * Time: 3:46 PM
 * To change this template use File | Settings | File Templates.
 */
public class SimulationHandler {
    private final Messages messages = GWT.create(Messages.class);
    private final ImageResources images = GWT.create(ImageResources.class);

    private int initMoney;
    private int windowMoney;
    private int partyMoney;
    private int garbageMoney;
    private int rent;
    private int currentMoneyPlayer1;
    private int currentMoneyPlayer2;
    private Choice windowChoice;
    private Choice partyChoice;
    private Choice garbageChoice;
    private Stack<Choice> stack;
    private ImageResource image;

    public SimulationHandler() {

    }

    public void init() {
        partyMoney = Integer.parseInt(messages.partyMoney());
        windowMoney = Integer.parseInt(messages.windowMoney());
        initMoney = Integer.parseInt(messages.initMoney());
        garbageMoney = Integer.parseInt(messages.garbageMoney());
        rent = Integer.parseInt(messages.rent());
        currentMoneyPlayer1 = initMoney;
        currentMoneyPlayer2 = initMoney;


        windowChoice = new Choice("window");
        garbageChoice = new Choice("garbage");
        partyChoice = new Choice("party");
        stack = new Stack<Choice>();
        populateStack();
    }

    public Choice simulate() {
        if (currentMoneyPlayer1 <= 0 && currentMoneyPlayer2 <= 0)
            return null;
        if (stack.empty()) {
            currentMoneyPlayer1 -= 400;
            currentMoneyPlayer2 -= 400;
            if (currentMoneyPlayer1 <= 0 && currentMoneyPlayer2 <= 0)
                return null;
            populateStack();
        }
        Choice currentChoice = stack.pop();
        if (currentChoice.getName().equals("garbage")) {
            if (currentChoice.getFirstPlayerChoice().equals("Yes"))
                currentMoneyPlayer1 -= 50;
            if (currentChoice.getSecondPlayerChoice().equals("Yes")) {
                currentMoneyPlayer2 -= 50;
            }
        } else {
            if (currentChoice.getFirstPlayerChoice().equals("Yes"))
                currentMoneyPlayer1 -= 25;
            if (currentChoice.getSecondPlayerChoice().equals("Yes")) {
                currentMoneyPlayer2 -= 25;
            }
        }
        setCurrentImage(currentChoice);
        return currentChoice;
    }

    private void populateStack() {
        Choice.getMutualChoice(windowChoice);
        Choice.getMutualChoice(partyChoice);
        Choice.getMutualChoice(garbageChoice);
        stack.push(windowChoice);
        stack.push(garbageChoice);
        stack.push(partyChoice);
    }

    private void setCurrentImage(Choice currentChoice) {
        if (currentChoice.getName().equals("garbage")) {
            if (currentChoice.getFirstPlayerChoice().equals("Yes") || currentChoice.getSecondPlayerChoice().equals("Yes")) {
                setImage(images.noGarbage());
            } else {
                setImage(images.isGarbage());
            }
            return;
        }
        if (currentChoice.getName().equals("window")) {
            if (currentChoice.getFirstPlayerChoice().equals("Yes") || currentChoice.getSecondPlayerChoice().equals("Yes")) {
                setImage(images.isWindow());
            } else {
                setImage(images.noWindow());
            }
            return;
        }
        if (currentChoice.getName().equals("party")) {
            if (currentChoice.getFirstPlayerChoice().equals("Yes") || currentChoice.getSecondPlayerChoice().equals("Yes")) {
                setImage(images.isParty());
            } else {
                setImage(images.noParty());
            }
        }
    }

    public int getCurrentMoneyPlayer1() {
        return currentMoneyPlayer1;
    }

    public void setCurrentMoneyPlayer1(int currentMoneyPlayer1) {
        this.currentMoneyPlayer1 = currentMoneyPlayer1;
    }

    public int getCurrentMoneyPlayer2() {
        return currentMoneyPlayer2;
    }

    public void setCurrentMoneyPlayer2(int currentMoneyPlayer2) {
        this.currentMoneyPlayer2 = currentMoneyPlayer2;
    }

    public ImageResource getImage() {
        return image;
    }

    public void setImage(ImageResource image) {
        this.image = image;
    }
}
